package com.example.service;

import com.example.dao.UserDao;
import com.example.dto.UserDto;
import com.example.entity.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author guochao
 * @version 1.0
 * @date 2021/12/13 16:48
 */
@Service
public class UserService {
    @Resource
    private UserDao userDao;

    public void listPage(UserDto userDto) {
        PageHelper.startPage(userDto.getPageNumber(), userDto.getPageSize());
        List<UserDto> list = userDao.list(userDto);
        PageInfo<UserDto> pageInfo = new PageInfo<>(list);

        userDto.setTotal(pageInfo.getTotal());
        userDto.setRows(list);
    }

    public List<UserDto> list(UserDto userDto){
        return userDao.list(userDto);
    }

    public List<UserDto> listAll(){
        return list(null);
    }

    public UserDto getById(Long id) {
        return userDao.getById(id);
    }

    public void save(User user) {
        if (user.getId() != null) {
            user.setUpdateTime(new Date());
            userDao.update(user);
        }else{
            user.setCreateTime(new Date());
            userDao.add(user);
        }
    }

    public void delete(String id) {
        User user = new User();
        user.setId(id);
        user.setDeleteTime(new Date());
        userDao.delete(user);
    }
}
